﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RoleHelper
{
    class insertarbd
    {
        private string source;
        private string tabla;
        private string query;
        private string server;
        private string database;
        private string user;
        private string password;
        private string port;
        private MySql.Data.MySqlClient.MySqlConnection con;
        private List<string> campos;
        private List<string> datos;
        //IP oficial:84.120.224.213;
        //Connection string:source = "Server=host; Database=schema; Uid=user; Pwd=contraseña; Port=3306;";
        
        
        //Para conectar a la base de datos oficial (PC Tauri);
        public insertarbd(string dtbase, string usr, string pass)
        {
            server = "84.120.224.213";
            port = "3306";
            database = dtbase;
            user = usr;
            password = pass;
        }
        //Opcion no gracias soy pro;
        public insertarbd(string servr, string dtbase, string usr, string pass, string p)
        {
            server = servr;
            database = dtbase;
            user = usr;
            password = pass;
            port = p;
        }

        ~insertarbd()
        {
            campos.Clear();
            datos.Clear();
            cerrar();
        }
        //De momento solo inserta strings.
        //TODO: diferenciar cuando es un int para que no reviente la bd.
        public void crearquery(List<string> fields, List<string> data, string table)
        {
            campos = fields;
            datos = data;
            tabla = table;
            query = "INSERT INTO " + tabla + "(";
            for (int i = 0; i < fields.Count; i++)
            {
                if (i == fields.Count - 1)
                {
                    query += fields[i] + ")";
                }
                else
                {
                    query += fields[i] + ", ";
                }
            }
            query += " VALUES (";
            for (int i = 0; i < data.Count; i++)
            {
                if (i == data.Count - 1)
                {
                    query += "'" + data[i] + "');";
                }
                else
                {
                    query += "'" + data[i] + "', ";
                }
            }
        }

        public bool conectar()
        {
            try
            {
                //Connection string:source = "Server=host; Database=schema; Uid=user; Pwd=contraseña; Port=3306;";
                source = "Server=" + server + "; Database=" + database + "; Uid=" + user + "; Pwd=" + password + "; Port="+port+";";
                con = new MySql.Data.MySqlClient.MySqlConnection(source);
                con.Open();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public bool ejecutar()
        {
            try
            {
                MySql.Data.MySqlClient.MySqlCommand cmd = new MySql.Data.MySqlClient.MySqlCommand(query, con);
                cmd.ExecuteNonQuery();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public bool cerrar()
        {
            try
            {
                con.Close();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public string getquery()
        {
            return query;
        }
    }
}
